package top.shiyiri.singletonmode;

/**
 * 使用同步机制将单例设计模式中的懒汉式改写为线程安全的
 *
 *
 * @author Aunean
 * @date 2021/12/25
 */
class Bank {

    private Bank() {}

    private static Bank instance = null;

    // 方式一：
//    public static synchronized Bank getInstance() {
//
//        if (instance == null) {
//            instance = new Bank();
//        }
//        return instance;
//    }


    public static Bank getInstance() {
        //方式二：效率稍差
//        synchronized (Bank.class) {
//            if (instance == null) {
//                instance = new Bank();
//            }
//            return instance;
//        }
        //方式三：效率更高
        if (instance == null) {
            synchronized (Bank.class) {
                if (instance == null) {
                    instance = new Bank();
                }
                return instance;
            }
        }
        return instance;
    }

}


public class BankTest {
}
